package com.lijun.SpringBootJavaFX.controller;

import com.lijun.SpringBootJavaFX.ApplicationMain;
import com.sun.javafx.webkit.WebConsoleListener;
import de.felixroske.jfxsupport.FXMLController;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import netscape.javascript.JSObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ResourceBundle;
@FXMLController
public class WebView2Controller implements Initializable{

    Logger logger = LoggerFactory.getLogger(WebView2Controller.class);

    @FXML
    private WebView webView2;

    @Autowired
    ApplicationMain main;

    private WebEngine webEngine;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub

        webEngine = webView2.getEngine();
        String url = getClass().getResource("/html/hellow.html").toExternalForm();
        webEngine.load(url);

        webEngine.setJavaScriptEnabled(true);
        //js的alert监听事件
        webEngine.setOnAlert(new EventHandler<WebEvent<String>>() {
            @Override
            public void handle(WebEvent<String> event) {
                String data = event.getData();
                System.out.println("alert:"+data);
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setHeaderText("This is an alert!");
                alert.setContentText(data);
                alert.showAndWait();
            }
        });
        webEngine.getLoadWorker().stateProperty().addListener(
            (ObservableValue<? extends Worker.State> ov, Worker.State oldState,
             Worker.State newState) -> {
                if (newState == Worker.State.SUCCEEDED) {
                    //讲本controller对象嵌入html的window对象，作为app对象使用，js中通过app.可以调用此controller中的方法
                    JSObject win = (JSObject) webEngine.executeScript("window");
                    win.setMember("app", this);
                }
            }
        );

        //添加console监听器
        WebConsoleListener.setDefaultListener(
            (WebView webView, String message, int lineNumber, String sourceId) ->{
                System.out.println("Console: " + message + " [" + sourceId + ":" + lineNumber + "] ");
            }
        );

        //获取住stage
//        Stage primaryStage = main.getPrimaryStage();
//        webEngine.setOnResized(
//            new EventHandler<WebEvent<Rectangle2D>>() {
//                public void handle(WebEvent<Rectangle2D> ev) {
//                    Rectangle2D r = ev.getData();
//                    primaryStage.setWidth(r.getWidth());
//                    primaryStage.setHeight(r.getHeight());
//                }
//            }
//        );

    }

    public void test(String value){
        logger.info(value);
    }

}
